// 频道的数据操作
import { getUserChannels, getChannels } from '@/api/channels'

export default {
  namespaced: true,
  state: {
    channelList: [],
    allchannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.channelList = payload
    },
    setAllChannelList(state, payload) {
      state.allchannelList = payload
    },
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    delChannelList(state, payload) {
      state.channelList = state.channelList.filter(
        item => item.id !== payload.id
      )
    }
  },
  actions: {
    async actions_getChannelList(context) {
      const res = await getUserChannels()
      console.log(res)
      context.commit('setChannelList', res.data.channels)
    },
    async actions_AllgetChannelList(context) {
      const res = await getChannels()
      console.log('res', res)
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    optionsChannelList(state) {
      return state.allchannelList.filter(item => {
        const res = state.channelList.some(channel => {
          return channel.id === item.id
        })
        console.log('res', res)
        // true => 表示该频道已经在我的频道有了
        // false => 属于可选频道

        if (res) {
          return false
        } else {
          return true // 符合条件为true，就会被过滤出去
        }
      })
    }
  }
}
